package com.ysd.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.ysd.entity.ProductclassifiCationTable;
import com.ysd.entity.Productdetailsinfo;
import com.ysd.entity.User;
import com.ysd.util.DBManager;

public class UserDaoImpl {
	public int addMessage(User user) {
		String sql = "insert into user (uid,username,phone,email,content)values(null,'"+user.getUsername()+"','"+user.getPhone()+"','"+user.getEmail()+"','"+user.getContent()+"') ";
		System.out.println(sql);
		return DBManager.executeUpdate(sql);
	}
	public int updaMessage(User user) {
		return DBManager.executeUpdate("update user set status='"+user.getStatus()+"' where uid = "+user.getUid());
	}
	public int updateMessage(User user) {
		return DBManager.executeUpdate("update user set status='已回复',reply='"+user.getReply()+"' where uid = "+user.getUid());
	}
	
	
	public List<User> getuser(String status){
		List<User> list = new ArrayList<User>();
		Connection conn = DBManager.grtConnection();
		Statement st = null;
		ResultSet rs = null;
		try {
			st = conn.createStatement();
			String sql = "select * from User where 1=1 ";
			if (status!=null&&!status.equals("")) {
				sql +="and status = '"+status+"'";
			}
			sql+= "  order by date desc";
			System.out.println(sql);
			st.executeQuery(sql);
			rs = st.executeQuery(sql);
			while(rs.next()) {
				int id = rs.getInt("uid");
				String username = rs.getString("username");
				String phone = rs.getString("phone");
				String email = rs.getString("email");
				String content = rs.getString("content");
				String time = rs.getString("date");
				String sta = rs.getString("status");
				User User = new User(username, phone, email, content, id, time, sta);
				list.add(User);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	} 
	/**
	 * 分页
	 * 
	 * */
	public List<User> getUserbypage(String status,int page ,int size){
		List<User> list = new ArrayList<User>();
		Connection conn = DBManager.grtConnection();
		Statement st = null;
		ResultSet rs = null;
		try {
			st = conn.createStatement();
			String sql = "select * from User where 1=1 ";
			if (status!=null&&!status.equals("")) {
				sql +="and status = '"+status+"'";
			}
			sql+= "  order by date desc";
			sql += " limit "+(page-1)*size+","+size;
			System.out.println(sql);
			st.executeQuery(sql);
			rs = st.executeQuery(sql);
			while(rs.next()) {
				int id = rs.getInt("uid");
				String username = rs.getString("username");
				String phone = rs.getString("phone");
				String email = rs.getString("email");
				String content = rs.getString("content");
				String time = rs.getString("date");
				String sta = rs.getString("status");
				User User = new User(username, phone, email, content, id, time, sta);
				list.add(User);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	} 
	
	public User getUserById(int id) {
		User User = null;
		Connection conn = DBManager.grtConnection();
		Statement st = null;
		ResultSet rs = null;
		try {
			st = conn.createStatement();
			rs= st.executeQuery("select * from User where uid="+id);
			while (rs.next()) {
				String username = rs.getString("username");
				String phone = rs.getString("phone");
				String email = rs.getString("email");
				String content = rs.getString("content");
				String time = rs.getString("date");
				String sta = rs.getString("status");
				User = new User(username, phone, email, content, id, time, sta);
				}
			return User;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	
	public int delliuyan(int cId) {
		return DBManager.executeUpdate("DELETE from user where uid = "+cId);
	}
	
}
